package org.fyv233.missingno_craft.item.custom;

import net.minecraft.block.BlockState;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.fyv233.missingno_craft.block.ModBlocks;

import java.util.List;

public class Prospecter extends Item {
    public Prospecter(Settings settings) {
        super(settings);
    }

    @Override
    public ActionResult useOnBlock(ItemUsageContext context) {
        World world = context.getWorld();
        PlayerEntity player = context.getPlayer();
        if (!world.isClient()) {
            BlockPos pos = context.getBlockPos();
            boolean foundBlock = false;
            for (int i = -64; i <= pos.getY() + 64; i++) {
                BlockState state = world.getBlockState(pos.down(i));
                if (isRightBlock(state)) {
                    outputMessage(pos.down(i), player);
                    foundBlock = true;
                    break;
                }
            }
            if(!foundBlock) {
                player.sendMessage(Text.translatable("text.missingno_craft.prospecter.output.notfound"));
            }
            context.getStack().damage(10, player, EquipmentSlot.MAINHAND);
        }
        return super.useOnBlock(context);
    }

    private boolean isRightBlock(BlockState state) {
        return state.isOf(ModBlocks.MISSINGNO_ORE);
    }


    private void outputMessage(BlockPos pos, PlayerEntity player) {
        player.sendMessage(Text.translatable("text.missingno_craft.prospecter.output.found", Text.of("[" + pos.getX() + "," + pos.getY() + "," + pos.getZ() + "]")), false);
    }

    @Override
    public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type){
        super.appendTooltip(stack, context, tooltip, type);
        tooltip.add(Text.translatable("text.missingno_craft.missingno_prospecter.tooltip"));
    }
}
